package com.fjh.service;

import com.fjh.pojo.Activity;
import com.fjh.pojo.User;
import com.fjh.repository.ActivityRepository;
import com.fjh.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;


@Service
@Transactional
public class UserService {

    @Autowired
    private UserRepository userRepository;

    // 新增用户
    public User addUser(User user) {
        return userRepository.save(user);
    }

    // 根据用户名或ID查询用户
    public Optional<User> findUser(String keyword) {
        try {
            Integer userId = Integer.parseInt(keyword);
            return userRepository.findById(userId);
        } catch (NumberFormatException e) {
            return Optional.ofNullable(userRepository.findByLoginName(keyword));
        }
    }

    // 获取所有用户
    public List<User> getAllUsers() {
        return userRepository.findAll();
    }
}
